/**
 * Copyright (c) 2014, Dallas.Cao 曹斌 (adffice@gmail.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.adffice.library.dbhelper.sqlite;

import android.database.Cursor;
import android.util.Log;

import com.adffice.library.dbhelper.table.Property;
import com.adffice.library.dbhelper.table.TableInfo;

import java.util.Date;

/**
 * Created by dallas on 14-5-3.
 */
public class CursorUtils {
    public static <T> T getEntity(Cursor cursor, TableInfo tableInfo) {

        try {
            if (cursor != null && cursor.getColumnCount() > 0) {
                T entity = (T) tableInfo.getEntityClazz().newInstance();

                for (int i = 0; i < cursor.getColumnCount(); i++) {
                    String column = cursor.getColumnName(i);

                    Property property = tableInfo.propertyMap.get(column);
                    if (property == null) {
                        if (tableInfo.getId().getColumn().equals(column)) {
                            property = tableInfo.getId();
                        }
                    }
                    if (property != null) {
                        if (property.getDataType().equals(String.class)) {
                            property.setValue(entity, cursor.getString(i));
                        } else if (property.getDataType().equals(Integer.class) || property.getDataType().equals(int.class)) {
                            property.setValue(entity, cursor.getInt(i));
                        } else if (property.getDataType().equals(Byte.class) || property.getDataType().equals(byte.class)) {
                            property.setValue(entity, (byte) cursor.getInt(i));
                        } else if (property.getDataType().equals(Long.class) || property.getDataType().equals(long.class)) {
                            property.setValue(entity, cursor.getLong(i));
                        } else if (property.getDataType().equals(Double.class) || property.getDataType().equals(double.class)) {
                            property.setValue(entity, cursor.getDouble(i));
                        } else if (property.getDataType().equals(Float.class) || property.getDataType().equals(float.class)) {
                            property.setValue(entity, cursor.getFloat(i));
                        } else if (property.getDataType().equals(Short.class) || property.getDataType().equals(short.class)) {
                            property.setValue(entity, cursor.getShort(i));
                        } else if (property.getDataType().equals(Byte[].class) || property.getDataType().equals(byte[].class)) {
                            property.setValue(entity, cursor.getBlob(i));
                        } else if (property.getDataType().equals(Boolean.class) || property.getDataType().equals(boolean.class)) {
                            Boolean value = cursor.getInt(i) == 1;
                            property.setValue(entity, value);
                        } else if (property.getDataType().equals(Character.class) || property.getDataType().equals(char.class)) {
                            Character value = cursor.getString(i) == null ? null : cursor.getString(i).charAt(0);
                            property.setValue(entity, value);
                        } else if (property.getDataType().equals(Date.class)) {
                            Date value = cursor.getLong(i) == 0 ? null : new Date(cursor.getLong(i));
                            property.setValue(entity, value);
                        } else {
                            Log.e("dbhelper", "nonsupport data type " + property.getDataType());
                        }
                    }
                }
                return entity;
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
